﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.Person;
using BusinessObjects.Person;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer.Person
{
    public class SqlServerAccountingObjectCategoryDao : IAccountingObjectCategoryDao
    {
        public List<AccountingObjectCategory> GetAccountingObjectCategories()
        {
            string procedures = "Get_All_AccountingObjectCategories";

            return Db.ReadList(procedures, true, Make);
        }

        public AccountingObjectCategory GetAccountingObjectCategoryByBranch(string restaurantId, string accountingObjectCategoryId)
        {
            string procedures = "Get_All_AccountingObjectCategory_ByBranch";

            object[] parms = { "@RestaurantID", restaurantId,
                                "@AccountingObjectCategoryId", accountingObjectCategoryId  };
            return Db.Read(procedures, true, Make, parms); ;
        }

        public List<AccountingObjectCategory> GetAccountingObjectCategoriesByBranch(string restaurantId, bool isCustomer, bool isVendor, bool isEmployee)
        {
            string procedures = "Get_All_AccountingObjectCategories_ByBranch";

            object[] parms = { "@RestaurantID", restaurantId,
                               "@IsCustomer", isCustomer,
                               "@IsVendor", isVendor,
                               "@IsEmployee", isEmployee };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public AccountingObjectCategory GetAccountingObjectCategory(string accountingObjectCategoryId)
        {
            string procedures = "Get_All_AccountingObjectCategory_ById";

            object[] parms = { "@AccountingObjectCategoryID", accountingObjectCategoryId };
            return Db.Read(procedures, true, Make, parms);
        }

        public List<AccountingObjectCategory> GetAccountingObjectCategoriesByBranchAndIsActive(string restaurantId, bool isCustomer, bool isVendor, bool isEmployee, bool isActive)
        {
            string procedures = "Get_All_AccountingObjectCategories_ByBranch_AndIsActive";

            object[] parms = {  "@RestaurantId", restaurantId,
                                "@IsActive", isActive,
                                "@IsCustomer", isCustomer,
                                "@IsVendor", isVendor,
                                "@IsEmployee", isEmployee };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public int GetCountAcountingObjectCategoryByParentId(string parentId)
        {
            string procedures = "Proc_GetCount_AccountingObjectCategory_ByParentId";

            object[] parms = { "@ParentId", parentId };

            return Db.Count(procedures, true, parms);
        }

        public void InsertAccountingObjectCategory(AccountingObjectCategory accountingObjectCategory)
        {
            string sql = "Insert_AccountingObjectCategory";

            accountingObjectCategory.AccountingObjectCategoryID = Db.Insert(sql, true, Take(accountingObjectCategory));
        }

        public void UpdateAccountingObjectCategory(AccountingObjectCategory accountingObjectCategory)
        {
            string sql = "Update_AccountingObjectCategory";

            Db.Update(sql, true, Take(accountingObjectCategory));
        }

        public void DeleteAccountingObjectCategory(AccountingObjectCategory accountingObjectCategory)
        {
            string sql = "Delete_AccountingObjectCategory";

            Db.Update(sql, true, Take(accountingObjectCategory.AccountingObjectCategoryID));
        }

        private static Func<IDataReader, AccountingObjectCategory> Make = reader =>
            new AccountingObjectCategory
            {
                AccountingObjectCategoryID = reader["AccountingObjectCategoryID"].AsString(),
                AccountingObjectCategoryCode = reader["AccountingObjectCategoryCode"].AsString(),
                AccountingObjectCategoryName = reader["AccountingObjectCategoryName"].AsString(),
                ParentID = reader["ParentID"].AsString(),
                IsParent = reader["IsParent"].AsBool(),
                IsCustomer = reader["IsCustomer"].AsBool(),
                IsVendor = reader["IsVendor"].AsBool(),
                IsEmployee = reader["IsEmployee"].AsBool(),
                RestaurantID = reader["RestaurantID"].AsString(),
                Description = reader["Description"].AsString(),
                IsActive = reader["IsActive"].AsBool()
            };

        private object[] Take(AccountingObjectCategory accountingObjectCategory)
        {
            return new object[]  
            {
                "@AccountingObjectCategoryID", accountingObjectCategory.AccountingObjectCategoryID,
                "@AccountingObjectCategoryCode", accountingObjectCategory.AccountingObjectCategoryCode,
                "@AccountingObjectCategoryName", accountingObjectCategory.AccountingObjectCategoryName,
                "@ParentID", accountingObjectCategory.ParentID,
                "@IsParent", accountingObjectCategory.IsParent,
                "@IsCustomer", accountingObjectCategory.IsCustomer,
                "@IsVendor", accountingObjectCategory.IsVendor,
                "@IsEmployee", accountingObjectCategory.IsEmployee,
			    "@RestaurantID", accountingObjectCategory.RestaurantID,
                "@Description", accountingObjectCategory.Description ,
                "@IsActive", accountingObjectCategory.IsActive
            };
        }

        private object[] Take(string accountingObjectCategoryID)
        {
            return new object[]  
            {
                "@AccountingObjectCategoryID", accountingObjectCategoryID,
            };
        }
    }
}
